package com.qf.service.serviceImpl;

import com.qf.dao.UserMapper;
import com.qf.pojo.BaseResp;
import com.qf.service.SendPhone;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
// 导入 SMS 模块的 client
import com.tencentcloudapi.sms.v20190711.SmsClient;
// 导入要请求接口对应的 request response 类
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class SendPhoneImpl implements SendPhone {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseResp send(String phone) {
        try {

            Credential cred = new Credential("AKIDDDyB9qnAYfcEk8PIXWt4zF4tNefQba2F", "2oMdeOuCKWaQ0JODo9DguswlttcAIuxw");

            HttpProfile httpProfile = new HttpProfile();

            httpProfile.setReqMethod("POST");

            httpProfile.setConnTimeout(60);

            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();

            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "",clientProfile);

            SendSmsRequest req = new SendSmsRequest();

            String appid = "1400486894";
            req.setSmsSdkAppid(appid);

            String sign = "总想挂三挡";
            req.setSign(sign);

            String templateID = "874902";
            req.setTemplateID(templateID);

            String[] phoneNumbers = {"+86"+phone};
            req.setPhoneNumberSet(phoneNumbers);

            Random random = new Random();
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0;i<4;i++){
                stringBuffer.append(random.nextInt(10));
            }


            String[] templateParams = {stringBuffer.toString(),"5"};
            req.setTemplateParamSet(templateParams);

            SendSmsResponse res = client.SendSms(req);

            redisTemplate.opsForValue().set(phone,stringBuffer.toString(),360, TimeUnit.SECONDS);


        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        BaseResp baseResp =new BaseResp();
        baseResp.setMgs("发送成功");
        return baseResp;
    }

    @Override
    public Boolean select(String phone, String code) {

        String o =(String) redisTemplate.opsForValue().get(phone);
        if (o!=null&o.equals(code)){
            return true;
        }
        return false;
    }

}
